<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;

/**
 * 回收站
 * @icon fa fa-user
 */
class Budrecycle extends Backend
{
    // 快速查询，搜索指定的哪些字段
    protected $searchFields = 'id,nickname,scource.name';
    protected $relationSearch = true;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Business.Business');
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->onlyTrashed()
                ->with(['scource', 'admin'])
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->onlyTrashed()
                ->with(['scource', 'admin'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 删除
     */
    public function del($ids = null)
    {
        $row = $this->model->onlyTrashed()->select($ids);
        if (empty($row)) {
            $this->error('当前订单不存在');
        }
        foreach($row as $item){
            $img=$item['avatar']??'';
            if($img){
                is_file(ltrim($img, '/')) && unlink(ltrim($img, '/'));
                delImg($img);
            }
        }
        $result = $this->model->destroy($ids, true);
        if (!$result) {
            $this->error($this->model->getError());
        }
        $this->success();
    }
    /*
    还原
    */
    public function reduction($ids = null)
    {
        $ids = explode(',', $ids);
        $dataList = [];
        foreach ($ids as $id) {
            $res = $this->model->onlyTrashed()->find($id);
            if (!$res) {
                $this->error('未找当前用户');
            }
            $data = [
                'id' => $id,
                'deletetime' => null
            ];
            $dataList[] = $data;
        }
        $res = $this->model->isUpdate()->saveAll($dataList);
        if ($res == 0) {
            $this->error($this->model->getError());
        }
        $this->success();
    }
}
